package ModelLayer;
/**
 * Objects of class OrderLine unites an Item object with a quantity.
 * An OrderLine object is used to add Item objects to an Order object.
 * 
 * @author  Mie P., Danny M.P., Jakob K., Anna K., Anders A.P.  
 * @version 1.0
 */

public class OrderLine {

    private int    quantity;
    private Item   item;
    private double linePrice;

    /**
     * Constructor for objects of class OrderLine
     * 
     * @param   item        the Item object.
     * @param   quantity    the quantity of the Item.
     * @throw   Exception   if the salesPrice of the Item is not set.
     */
    public OrderLine(Item item, int quantity) throws Exception {

        setItem(item);
        setQuantity(quantity);
        calculateLinePrice(item, quantity);

    }

    /**
     * Associates an Item object with the OrderLine object.
     *
     * @param   i   an Item object.
     */
    private void setItem(Item i) {

        this.item = i;

    }

    /**
     * Sets the OrderLine object's quantity field.
     *
     * @param   input   quantity as an integer value.
     */
    public void setQuantity(int input) {

        this.quantity = input;

    }

    /**
     * Calculates the line price.
     * 
     * @throw   Exception   if the salesPrice of the Item is not set.
     */
    private void calculateLinePrice(Item i, int quantity) throws Exception {

        if (i instanceof Product) {
            Product p = (Product) i;
            if (quantity >= p.getQuantityInBulk()) {
                int noOfBulks = (quantity / p.getQuantityInBulk());
                int rest = (quantity % p.getQuantityInBulk());
                linePrice = (noOfBulks * p.getSalesPriceBulk()) + (rest * p.getSalesPrice());
            }
            else {
                linePrice = p.getSalesPrice() * quantity;
            }
        }
        else {
            linePrice = i.getSalesPrice() * quantity;
        }

    }

    /**
     * Returns the Item object associated with the orderline.
     * 
     * @return  item    an Item object associated to the OrderLine object.
     */
    public Item getItem() {

        return this.item;

    }

    /**
     * Returns OrderLine object's quantity field to the caller.
     * 
     * @return  quantity    the value of the quantity.
     */
    public int getQuantity() {

        return this.quantity;

    }

    /**
     * Returns Orderline object's linePrice field to the caller.
     * 
     * @return  linePrice   the calculated linePrice (salesPrice) of the OrderLine object.
     */
    public double getLinePrice() {

        return this.linePrice;

    }

}